package com.hr.controller;

import com.hr.entity.Employee;
import com.hr.entity.LoginUser;
import com.hr.service.EmployeeService;
import com.hr.service.LoginService;
import com.hr.utils.ResultInfo;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EmployeeController
 * @Description TODO
 * @Author hkp
 **/

@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/emp/findAll")
    @ResponseBody
    public Map<String,Object> queryAllEmp(){
        Map<String, Object> map = new HashMap<>();
        List<Employee> list = employeeService.queryAllEmp();
        map.put("EmployeeList", list);
        if (list != null) {
            return ResultInfo.success(map).put("msg", "查询成功!");
        } else {
            return ResultInfo.fail("查询失败!");
        }
    }

    @RequestMapping(value = "/emp/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addEmp(@RequestBody Map<String,Object> map){
        String eId = (String) map.get("eId");
        String eName = (String) map.get("eName");
        String eSex = (String) map.get("eSex");
        String eTel = (String) map.get("eTel");
        String eDepartment = (String) map.get("eDepartment");
        String ePosition = (String) map.get("ePosition");
        String ePostState = (String) map.get("ePostState");
        String tmp_eHireDate = (String) map.get("eHireDate");

        //日期转换
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh");
        Date eHireDate = null;
        try {
            eHireDate = sdf.parse(tmp_eHireDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //对象封装
        Employee employee = new Employee(eId,eName,eSex,eTel,eDepartment,ePosition,ePostState,eHireDate,null);
        int res = employeeService.addEmp(employee);
        if(res < 0){
            return ResultInfo.fail("添加员工失败，请稍后再试");
        }else{
            return ResultInfo.success("添加员工成功！");
        }
    }

    @RequestMapping(value = "/emp/del/{id}", method = RequestMethod.DELETE)
    @ResponseBody //不加这个注释报错
    public Map<String,Object> deleteEmp(@PathVariable("id") String id){
        int res = employeeService.delEmp(Integer.parseInt(id));
        if(res < 0) {
            return ResultInfo.fail("删除员工失败，请稍后再试");
        }else{
            return ResultInfo.success("删除员工成功！");
        }
    }

    @RequestMapping(value = "/emp/update", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String,Object> updateEmp(@RequestBody Map<String,Object> map){
        String id = (String) map.get("id");
        String eId = (String) map.get("eId");
        String eName = (String) map.get("eName");
        String eSex = (String) map.get("eSex");
        String eTel = (String) map.get("eTel");
        String eDepartment = (String) map.get("eDepartment");
        String ePosition = (String) map.get("ePosition");
        String ePostState = (String) map.get("ePostState");
        String tmp_eHiredate = (String) map.get("eHiredate");
        String tmp_eLeavedate = (String) map.get("eLeavedate");

        //日期转换
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date eHiredate = null;
        Date eLeavedate = null;
        try {
            if(!StringUtils.isNullOrEmpty(tmp_eHiredate)) eHiredate = sdf.parse(tmp_eHiredate);
            if(!StringUtils.isNullOrEmpty(tmp_eLeavedate)) eLeavedate = sdf.parse(tmp_eLeavedate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //对象封装
        Employee employee = new Employee(eId,eName,eSex,eTel,eDepartment,ePosition,ePostState,eHiredate,eLeavedate);
        int res = employeeService.updateEmp(employee);
        if(res < 0) {
            return ResultInfo.fail("更新员工信息失败，请稍后再试");
        }else{
            return ResultInfo.success("更新员工信息成功！");
        }
    }

    @RequestMapping(value = "/emp/query", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> queryEmp(@RequestBody Map<String,Object> map){
        String id = (String) map.get("id");
        Employee employee = employeeService.queryEmp(Integer.parseInt(id));
        if(employee == null) {
            return ResultInfo.fail("查询员工信息失败，请稍后再试");
        }else{
            return ResultInfo.success("查询员工信息成功！").put("employee",employee);
        }
    }


}
